var app = getApp()
var gb = app.globalData

var Define = require("../../lib/Define.js")
var Comment = require("../template/comments/comments_cell.js")

var __productId;
var __userProductId;

var __sharedTitle;

Page({
    data:{
        data:{},
        images:[],
        consumer:{},
        comments:{},
        likeCount:0,
        viewCount:0
    },
    onLoad:function(options){

        __productId = options.productId;
        __userProductId = options.userProductId

        if (__productId) {
          this.requestProductDetail();
        }
        if (__userProductId) {
          this.requestUserProduct()
        }

        var ofid = 0
        var oftb = null
        if (__productId) {
          ofid = __productId
          oftb = "tb_product"
        }
        else if (__userProductId) {
          ofid = __userProductId
          oftb = "tb_user_product"
        }
        var self = this
        Comment.likeCount(ofid,oftb,function(count){
          self.setData({
            likeCount:count
          })
        })

        Comment.viewCount(ofid, oftb, function (count) {
          self.setData({
            viewCount: count
          })
        })

    },
    onShow:function(){

      var ofid = 0
      var oftb = null
      if(__productId){
        ofid = __productId
        oftb = "tb_product"
      }
      else if(__userProductId){
        ofid = __userProductId
        oftb = "tb_user_product"
      }
      var self = this

      Comment.listComment(ofid, oftb, function (list) {
        self.setData({
          comments:list
        })
      })
      
    },
    requestUserProduct:function(){
        var self = this;

        wx.request({
          url: Define.value.baseUrl+"/app/user_product/userproductdetail?upId="+__userProductId,
          data: {},
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
            // success
            var data = res.data
            
            if(data.code == 200){
                var sendData = {}
                sendData.title = data.data.title
                sendData.subject = data.data.subject
                sendData.styleName = data.data.styleName
                sendData.uDateStr = Define.dateFrom(data.data.uDate,"yyyy-MM-dd")
                sendData.userRealName = data.data.userRealName
                sendData.userAvatar = data.data.userAvatar
                sendData.scanCount = data.data.scanCount
                self.setData({
                    data:sendData,
                    images:data.data.images
                })

                __sharedTitle = sendData.userRealName+"的照片"
            }
          },
          fail: function(res) {
            // fail
          },
          complete: function(res) {
            // complete
          }
        })
    },
    requestProductDetail:function(){
        var self = this;
        
        wx.request({
          url: Define.value.baseUrl+'/app/product/detail',
          data: {productId:__productId},
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          success: function(res){

            if(res.data.code == 200){
                var data = res.data.data;
                data.mainImage = data.images[0];
                data.uDateStr = Define.dateFrom(data.uDate,"yyyy-MM-dd")
                
                var images = []
                for(var index = 1 ; index < data.images.length ; index++){
                    var item = data.images[index]
                    images.push(item);
                }

                self.setData({
                    data:data,
                    images:images
                });

                __sharedTitle = data.title
            }
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
    },
    onShareAppMessage: function () {
    return {
      title: __sharedTitle,
      path: '/pages/proDetail/index?productId='+__productId+"&userProductId="+__userProductId,
      success: function(res) {
        // 分享成功
      },
      fail: function(res) {
        // 分享失败
      }
    }
  },
  onCommentBtnClick:function(){
    //点击评论，跳转至评论输入页面
    var data = {}
    if(__productId){
      data.ofId = __productId
      data.ofTb = "tb_product"
    }else{
      data.ofId = __userProductId
      data.ofTb = "tb_user_product"
    }

    Comment.onCommentBtnClick(data)
  },
  onLikeBtnClick:function(){
    var ofid = 0
    var oftb = null
    if (__productId) {
      ofid = __productId
      oftb = "tb_product"
    }
    else if (__userProductId) {
      ofid = __userProductId
      oftb = "tb_user_product"
    }
    var self = this

    Comment.like(ofid, oftb, function (count) {
      self.setData({
        likeCount:count
      })
    })
  }

})